import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;


public class SrvMessenger extends Thread
{
	Socket client = null;
	PrintWriter out = null;
	BufferedReader in = null;
	String message, outMessage;
	Serwer sv;
	
	SrvMessenger(Socket client, Serwer sv)
	{
		this.client=client;
		this.sv=sv;
	}
	public void run()
	{
		while(message!="Q")
		{
			try
			{
				in = new BufferedReader(new InputStreamReader(client.getInputStream()));
				out = new PrintWriter(client.getOutputStream(), true);
				message=in.readLine();
			}
			catch(IOException ex)
			{
				System.out.println("Blad odczytu");
				System.exit(0);
			}
			catch(NullPointerException ex)
			{
				System.out.println("Blad odczytu");
				System.exit(0);
			}
			try
			{
				if(message=="Q") client.close();
			}
			catch(IOException ex)
			{
				System.out.println("Nie udalo sie zakonczyc polaczenia z klientem");
				System.exit(0);
			}
			/*if(outMessage!="")
			{
				System.out.println(outMessage);
				out.println(outMessage);
				outMessage="";
			}*/
			if(message!="") sv.newMessage(message);
			
		}
	}

}
